package com.zzw.springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zzw.springboot.bean.SysDictData;
import com.zzw.springboot.bean.SysDictType;
import com.zzw.springboot.service.SysDictDataService;
import com.zzw.springboot.service.SysDictTypeService;
import com.zzw.springboot.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 字典信息控制器
 */
@RestController
@RequestMapping("/dict")
public class SysDictController {

    @Autowired
    private SysDictTypeService dictTypeService;

    @Autowired
    private SysDictDataService dictDataService;

    /**
     * 查询字典类型列表
     */
    @GetMapping("/type/list")
    public Result listType(SysDictType dictType) {
        LambdaQueryWrapper<SysDictType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(dictType.getDictName()), SysDictType::getDictName, dictType.getDictName())
                .like(StringUtils.isNotBlank(dictType.getDictType()), SysDictType::getDictType, dictType.getDictType())
                .eq(dictType.getStatus() != null, SysDictType::getStatus, dictType.getStatus())
                .orderByAsc(SysDictType::getDictId);
        List<SysDictType> list = dictTypeService.list(queryWrapper);
        return Result.success(list);
    }

    /**
     * 查询所有字典类型
     */
    @GetMapping("/type/all")
    public Result listAllType() {
        List<SysDictType> list = dictTypeService.list(new LambdaQueryWrapper<SysDictType>()
                .eq(SysDictType::getStatus, 1)
                .orderByAsc(SysDictType::getDictId));
        return Result.success(list);
    }

    /**
     * 获取字典类型详细信息
     */
    @GetMapping("/type/{dictId}")
    public Result getType(@PathVariable Integer dictId) {
        SysDictType dictType = dictTypeService.getById(dictId);
        return Result.success(dictType);
    }

    /**
     * 新增字典类型
     */
    @PostMapping("/type")
    public Result addType(@RequestBody SysDictType dictType) {
        boolean success = dictTypeService.save(dictType);
        return success ? Result.success("添加成功") : Result.error("添加失败");
    }

    /**
     * 修改字典类型
     */
    @PutMapping("/type")
    public Result updateType(@RequestBody SysDictType dictType) {
        boolean success = dictTypeService.updateById(dictType);
        return success ? Result.success("修改成功") : Result.error("修改失败");
    }

    /**
     * 删除字典类型
     */
    @DeleteMapping("/type/{dictId}")
    public Result deleteType(@PathVariable Integer dictId) {
        boolean success = dictTypeService.removeById(dictId);
        return success ? Result.success("删除成功") : Result.error("删除失败");
    }

    /**
     * 批量删除字典类型
     */
    @DeleteMapping("/type/batch/{dictIds}")
    public Result batchDeleteType(@PathVariable String dictIds) {
        List<Integer> ids = Arrays.stream(dictIds.split(","))
                .map(Integer::parseInt)
                .collect(Collectors.toList());
        boolean success = dictTypeService.removeByIds(ids);
        return success ? Result.success("批量删除成功") : Result.error("批量删除失败");
    }

    /**
     * 查询字典数据列表
     */
    @GetMapping("/data/list")
    public Result listData(SysDictData dictData) {
        LambdaQueryWrapper<SysDictData> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(dictData.getDictType()), SysDictData::getDictType, dictData.getDictType())
                .like(StringUtils.isNotBlank(dictData.getDictLabel()), SysDictData::getDictLabel, dictData.getDictLabel())
                .eq(dictData.getStatus() != null, SysDictData::getStatus, dictData.getStatus())
                .orderByAsc(SysDictData::getDictSort);
        List<SysDictData> list = dictDataService.list(queryWrapper);
        return Result.success(list);
    }

    /**
     * 获取字典数据详细信息
     */
    @GetMapping("/data/{dictCode}")
    public Result getData(@PathVariable Integer dictCode) {
        SysDictData dictData = dictDataService.getById(dictCode);
        return Result.success(dictData);
    }

    /**
     * 根据字典类型查询字典数据
     */
    @GetMapping("/data/type/{dictType}")
    public Result getDictData(@PathVariable String dictType) {
        List<SysDictData> dictData = dictDataService.selectDictDataByType(dictType);
        return Result.success(dictData);
    }

    /**
     * 新增字典数据
     */
    @PostMapping("/data")
    public Result addData(@RequestBody SysDictData dictData) {
        boolean success = dictDataService.save(dictData);
        return success ? Result.success("添加成功") : Result.error("添加失败");
    }

    /**
     * 修改字典数据
     */
    @PutMapping("/data")
    public Result updateData(@RequestBody SysDictData dictData) {
        boolean success = dictDataService.updateById(dictData);
        return success ? Result.success("修改成功") : Result.error("修改失败");
    }

    /**
     * 删除字典数据
     */
    @DeleteMapping("/data/{dictCode}")
    public Result deleteData(@PathVariable Integer dictCode) {
        boolean success = dictDataService.removeById(dictCode);
        return success ? Result.success("删除成功") : Result.error("删除失败");
    }

    /**
     * 批量删除字典数据
     */
    @DeleteMapping("/data/batch/{dictCodes}")
    public Result batchDeleteData(@PathVariable String dictCodes) {
        List<Integer> ids = Arrays.stream(dictCodes.split(","))
                .map(Integer::parseInt)
                .collect(Collectors.toList());
        boolean success = dictDataService.removeByIds(ids);
        return success ? Result.success("批量删除成功") : Result.error("批量删除失败");
    }
}
